// File generated with Tower of Babel version: 5.3-beta on 09/05/17
var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var rack;
(function (rack) {
    var _B = BABYLON;
    var _M = _B.Matrix.FromValues;
    var _Q = _B.Quaternion;
    var _V = _B.Vector3;
    function CONTIG(array, offset, begin, end) {
        for(var i = 0, len = 1 + end - begin; i < len; i++) {
            array[offset + i] = begin + i;
        }
    }
    function REPEAT(array, offset, nRepeats, val) {
        for(var i = 0; i < nRepeats; i++) {
            array[offset + i] = val;
        }
    }
    var _sceneTransitionName;
    var _overriddenMillis;
    var _overriddenSound;
    var _options;

    function initScene(scene, resourcesRootDir, positionOffset, sceneTransitionName, overriddenMillis, overriddenSound, options) {
        if (!resourcesRootDir) { resourcesRootDir = "./"; }
        function MOVE(mesh, positionOffset) {
            mesh.position.addInPlace(positionOffset);
            if (mesh.isWorldMatrixFrozen) mesh.freezeWorldMatrix();
        }

        _sceneTransitionName = sceneTransitionName;
        _overriddenMillis    = overriddenMillis;
        _overriddenSound     = overriddenSound;
        _options             = options;

        scene.autoClear = true;
        scene.clearColor    = new _B.Color3(.0509,.0509,.0509);
        scene.ambientColor  = new _B.Color3(0,0,0);
        scene.gravity = new _V(0,-9.81,0);

        // define materials before meshes
        defineMaterials(scene, resourcesRootDir);

        // instance all root meshes
        var mesh;
        mesh = new box("box", scene);
        if (positionOffset) MOVE(mesh, positionOffset);

        if (sceneTransitionName && matLoaded) {
            QI.SceneTransition.perform(sceneTransitionName, waitingMeshes, overriddenMillis, overriddenSound, options);
        }
    }
    rack.initScene = initScene;

    var waitingMeshes = [];
    var pendingTextures = 0;
    var texLoadStart = 0;
    function onTexturesLoaded(){
        if (--pendingTextures > 0) return;
        _B.Tools.Log("Texture Load delay:  " + ((_B.Tools.Now - texLoadStart) / 1000).toFixed(2) + " secs");
        if (_sceneTransitionName) QI.SceneTransition.perform(_sceneTransitionName, waitingMeshes, _overriddenMillis, _overriddenSound, _options);
        else {
            for (var i = 0, len = waitingMeshes.length; i < len; i++) {
                if (!waitingMeshes[i].initComplete) continue;
                if (typeof waitingMeshes[i].grandEntrance == "function") waitingMeshes[i].grandEntrance();
                else makeVisible(waitingMeshes[i]);
            }
        }
        waitingMeshes = [];
        _sceneTransitionName = null;
        matLoaded = true;
    }

    // QI.Mesh has similar method, using this to not require QI
    function makeVisible(mesh){
        var children = mesh.getChildMeshes();
        mesh.isVisible = true;
        for (var i = 0, len = children.length; i < len; i++) {
            children[i].isVisible = true;
        }
    }

    var aheadQueued = false;
    function matReadAhead(materialsRootDir) {
        if (aheadQueued) return;
        var txBuffer;
        var fName;

        aheadQueued = true;
    }
    rack.matReadAhead = matReadAhead;

    var matLoaded = false;
    function defineMaterials(scene, materialsRootDir) {
        if (!materialsRootDir) { materialsRootDir = "./"; }
        if (materialsRootDir.lastIndexOf("/") + 1  !== materialsRootDir.length) { materialsRootDir  += "/"; }
        if (typeof(QI) !== "undefined") QI.TimelineControl.initialize(scene);
        if (typeof(TOWER_OF_BABEL) !== "undefined") TOWER_OF_BABEL.Preloader.SCENE = scene;
        var loadStart = _B.Tools.Now;
        matReadAhead(materialsRootDir);
        var material;
        var texture;
        var txBuffer;

        material = scene.getMaterialByID("rack.Material.001");
        if (!material){
            material = new _B.StandardMaterial("rack.Material.001", scene);
            material.ambientColor  = new _B.Color3(.0144,.0144,.0144);
            material.diffuseColor  = new _B.Color3(.0043,.0043,.0043);
            material.emissiveColor = new _B.Color3(0,0,0);
            material.specularColor = new _B.Color3(.016,.016,.016);
            material.specularPower = 50;
            material.alpha =  1;
            material.backFaceCulling = true;
            material.checkReadyOnlyOnce = false;
            material.maxSimultaneousLights = 4;
        } else material.markDirty();

        matLoaded = pendingTextures === 0;
        if (!matLoaded) texLoadStart = _B.Tools.Now;
        _B.Tools.Log("rack.defineMaterials completed:  " + ((_B.Tools.Now - loadStart) / 1000).toFixed(2) + " secs");
    }
    rack.defineMaterials = defineMaterials;

    var box = (function (_super) {
        __extends(box, _super);
        function box(name, scene, materialsRootDir, source) {
            _super.call(this, name, scene, null, source, true);

            if (!materialsRootDir) { materialsRootDir = "./"; }
            defineMaterials(scene, materialsRootDir); //embedded version check
            var cloning = source && source !== null;
            var load = _B.Tools.Now;
            var geo = 0;
            var shape = 0;
            this.position.x  = -.0025;
            this.position.y  = 1117.4382;
            this.position.z  = 86.5871;
            this.rotation.x  = 0;
            this.rotation.y  = 0;
            this.rotation.z  = 0;
            this.scaling.x   = 1;
            this.scaling.y   = 1;
            this.scaling.z   = 1;

            this.id = this.name;
            this.billboardMode  = 0;
            this.isVisible  = false; //always false; evaluated again at bottom
            this.setEnabled(true);
            this.checkCollisions = false;
            this.receiveShadows  = false;
            this.castShadows  = false;
            this.initComplete = false;
            if (!cloning){
                geo = _B.Tools.Now;
                this.setVerticesData(_B.VertexBuffer.PositionKind, new Float32Array([
                    -299.2474,1116.7615,-686.6203,-299.2474,1066.8491,513.4326,-299.2474,1066.8491,-686.6203,279.2805,1116.7615,513.4326,299.2525,1066.8491,513.4326,279.2805,1066.8491,513.4326,299.2525,1116.7615,513.4326,299.2525,1066.8491,-686.6203,-279.24,1116.7615,-686.6203,-279.24,1066.8491,-686.6203,299.2525,-1117.4382,513.4326,279.2805,-1117.4382,-686.6203,279.2805,-1117.4382,513.4326,-299.2474,1116.7615,513.4326,-279.24,1116.7615,513.4326,-279.24,-1072.2168,-686.6203,-299.2474,-1117.4382,-686.6203
                    ,-279.24,-1117.4382,-686.6203,299.2525,-1072.2168,513.4326,299.2525,-1117.4382,-686.6203,279.2805,-1072.2168,513.4326,-299.2474,-1072.2168,-686.6203,-299.2474,-1117.4382,513.4326,-299.2474,-1072.2168,513.4326,-279.24,-1117.4382,513.4326,-279.24,-1072.2168,513.4326,299.2525,-1072.2168,-686.6203,279.2805,-1072.2168,-686.6203,299.2525,1116.7615,-686.6203,279.2805,1116.7615,-686.6203,279.2805,1066.8491,-686.6203,-279.24,1066.8491,513.4326,279.2805,-1072.2168,-536.6203,-279.24,-1072.2168,313.3797
                    ,-279.24,-1072.2168,-536.6203,279.2805,-142.2168,-536.6203,-279.24,-142.2168,313.3797,-279.24,-142.2168,-536.6203,279.2805,-2.7168,-536.6203,-279.24,-2.7168,313.3797,-279.24,-2.7168,-536.6203,279.2805,927.2832,-536.6203,-279.24,927.2832,513.4326,-279.24,927.2832,-536.6203,279.2805,-140.7168,-536.6203,-279.24,-140.7168,313.3797,-279.24,-140.7168,-536.6203,279.2805,-4.2168,-536.6203,-279.24,-4.2168,313.3797,-279.24,-4.2168,-536.6203,279.2805,928.7832,-536.6203
                    ,-279.24,928.7832,513.4326,-279.24,928.7832,-536.6203,279.2805,927.2832,513.4326,279.2805,-2.7168,313.3797,279.2805,-142.2168,313.3797,279.2805,-1072.2168,313.3797,279.2805,-140.7168,313.3797,279.2805,-4.2168,313.3797,279.2805,928.7832,513.4326
                ]),
                false);

                var _i;//indices & affected indices for shapekeys
                _i = new Uint32Array(270);
                _i.set([0,1,2,3,4,5,6,7,4,8,2,9,10,11,12,13,8,14,15,16,17,18,19,10,20,10,12,21,22,16,23,24,22,25,12,24,26,11,19,27,17,11,3,28,6,14,29,3,24,16
                ,22,12,17,24,28,30,7,29,9,30,13,31,1,5,14,3,15,20,25,9,5,31,26,30,27,27,5,20,25,9,31,15,2,21,18,7,26,21,1,23,23,31,25,20,4,18]);
                CONTIG(_i, 96, 32, 53);
                _i.set([51,42,54,48,39,55,45,36,52,41,43,49,38,40,46,35,37,0,13,1,3,6,4,6,28,7,8,0,2,10,19,11,13,0,8,15,21,16,18,26,19,20,18,10,21,23,22,23,25,24
                ,25,20,12,26,27,11,27,15,17,3,29,28,14,8,29,24,17,16,12,11,17,28,29,30,29,8,9,13,14,31,5,31,14,15,27,20,9,30,5,26,7,30,27,30,5,25,15,9,15,9
                ,2,18,4,7,21,2,1,23,1,31,20,5,4,32,56,33,35,55,36,38,54,39,41,53,42,44,57,45,47,58,48,50,59,51,53,59,51,54,58,48,55,57,45,52,50,41,49,47,38,46
                ,44,35], 118);
                this.setIndices(_i);

                this.setVerticesData(_B.VertexBuffer.NormalKind, new Float32Array([
                    -.5773,.5773,-.5773,-.7071,0,.7071,-.7071,0,-.7071,0,.7071,.7071,.7071,0,.7071,-.3015,.3015,.9045,.5773,.5773,.5773,.7071,0,-.7071,0,.7071,-.7071,-.3015,.3015,-.9045,.5773,-.5773,.5773,0,-.7071,-.7071,0,-.7071,.7071,-.5773,.5773,.5773,0,.7071,.7071,-.3015,.3015,-.9045,-.5773,-.5773,-.5773
                    ,0,-.7071,-.7071,.7071,0,.7071,.5773,-.5773,-.5773,-.3015,.3015,.9045,-.7071,0,-.7071,-.5773,-.5773,.5773,-.7071,0,.7071,0,-.7071,.7071,-.3015,.3015,.9045,.7071,0,-.7071,-.3015,.3015,-.9045,.5773,.5773,-.5773,0,.7071,-.7071,-.3015,.3015,-.9045,-.3015,.3015,.9045,0,1,0,0,1,0
                    ,0,1,0,0,.7071,.7071,0,.7071,-.7071,0,.7071,.7071,0,.7071,-.7071,0,.7071,.7071,0,.7071,-.7071,0,.7071,.7071,0,.7071,-.7071,0,.7071,.7071,0,.7071,.7071,0,.7071,-.7071,0,.7071,.7071,0,.7071,-.7071,0,.7071,.7071,0,.7071,-.7071,0,.7071,.7071
                    ,0,.7071,-.7071,0,.7071,.7071,0,.7071,-.7071,0,.7071,.7071,0,.7071,-.7071,0,1,0,0,.7071,-.7071,0,.7071,.7071,0,.7071,-.7071
                ]),
                false);

                geo = (_B.Tools.Now - geo) / 1000;
                this.setMaterialByID("rack.Material.001");
                this.subMeshes = [];
                new _B.SubMesh(0, 0, 60, 0, 270, this);
                if (scene._selectionOctree) {
                    scene.createOrUpdateSelectionOctree();
                }
            }
            if (this.postConstruction) this.postConstruction();
            this.initComplete = true;
            load = (_B.Tools.Now - load) / 1000;
            _B.Tools.Log("defined mesh: " + this.name + (cloning ? " (cloned)" : "") + " completed:  " + load.toFixed(2) + ", geometry:  " + geo.toFixed(2) + ", skey:  " + shape.toFixed(2) + " secs");
            if (matLoaded && !_sceneTransitionName){
                if (typeof this.grandEntrance == "function") this.grandEntrance();
                else makeVisible(this);

            } else waitingMeshes.push(this);
        }

        box.prototype.dispose = function (doNotRecurse) {
            _super.prototype.dispose.call(this, doNotRecurse);
            if (this.skeleton) this.skeleton.dispose();
        };
        return box;
    })(BABYLON.Mesh);
    rack.box = box;

    function freshenShadowRenderLists(scene) {
        var renderList = [];
        for (var i = 0; i < scene.meshes.length; i++){
            if (scene.meshes[i]["castShadows"])
                renderList.push(scene.meshes[i]);
        }

        for (var i = 0; i < scene.lights.length; i++){
            if (scene.lights[i]._shadowGenerator)
                scene.lights[i]._shadowGenerator.getShadowMap().renderList = renderList;
        }
    }
    rack.freshenShadowRenderLists = freshenShadowRenderLists;
})(rack || (rack = {}));